﻿using NHibernate.Mapping.ByCode;

namespace Citi.EmailSender.Domain.Mapping
{
    public class EmailMap : EntityMap<Email>
    {
        public EmailMap()
        {
            Property(x => x.Body, m => m.NotNullable(true));
            Property(x => x.CreatedDate, m => m.NotNullable(true));
            Property(x => x.Importance, m => m.NotNullable(true));
            Property(x => x.OnBehalfOfId, m => m.NotNullable(true));
            Property(x => x.SentOn);
            Property(x => x.Subject, m => { m.NotNullable(true); m.Length(255); });

            ManyToOne(x => x.OnBehalfOf, m => { m.Column("OnBehalfOfId"); m.Update(false); m.Insert(false); });

            Bag(x => x.EmailReceivers, c =>
            {
                c.Key(k =>
                {
                    k.Column("EmailId");
                });
                c.Cascade(Cascade.None);
                c.Table("EmailReceiver");
            }, cr => cr.OneToMany(om => om.Class(typeof(EmailReceiver))));


            Bag(x => x.Attachments, c =>
            {
                c.Key(k =>
                {
                    k.Column("EmailId");
                });
                c.Cascade(Cascade.None);
                c.Table("Attachment");
            }, cr => cr.OneToMany(om => om.Class(typeof(Attachment))));


            //Set(p => p.Attachments, m =>
            //{
            //    m.Inverse(true);
            //    m.Cascade(Cascade.All | Cascade.DeleteOrphans);
            //    m.Key(k =>
            //    {
            //        k.Column("EmailId");
            //        k.NotNullable(true);
            //    });
            //}, a => a.OneToMany()); ;

            //Set(p => p.EmailReceivers, m =>
            //{
            //    m.Inverse(true);
            //    m.Cascade(Cascade.All | Cascade.DeleteOrphans);
            //    m.Key(k =>
            //    {
            //        k.Column("Id");
            //        k.NotNullable(true);
            //    });
            //}, a => a.OneToMany()); ;
        }
    }
}
